# Maintainer: Patrick Stewart <patstew@gmail.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=jupyter_client
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=7.0.6
pkgrel=1
pkgdesc="The reference implementation of the Jupyter protocol (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://jupyter.org/"
license=('BSD')
depends=(
    ${MINGW_PACKAGE_PREFIX}-python
    ${MINGW_PACKAGE_PREFIX}-python-dateutil
    ${MINGW_PACKAGE_PREFIX}-python-entrypoints
    ${MINGW_PACKAGE_PREFIX}-python-jupyter_core
    ${MINGW_PACKAGE_PREFIX}-python-nest-asyncio
    ${MINGW_PACKAGE_PREFIX}-python-pyzmq
    ${MINGW_PACKAGE_PREFIX}-python-tornado
    ${MINGW_PACKAGE_PREFIX}-python-traitlets
)
makedepends=(
    ${MINGW_PACKAGE_PREFIX}-python-setuptools
    ${MINGW_PACKAGE_PREFIX}-python-setuptools-scm)
source=(https://github.com/jupyter/${_realname}/archive/v${pkgver}.tar.gz)
sha256sums=('40d89c7509e134b4be816ab69222f74b27b8a9610844b56538856c750090f858')

prepare() {
  cd "${srcdir}"
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root=${pkgdir} --optimize=0 --skip-build

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    sed -e '1 { s/^#!.*$// }' -i ${_f}
  done

  install -Dm644 "COPYING.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
